package com.kaoshi.myweb.snippet.mq.rabbitmq.publish;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.MessageProperties;

public class NewTask {

	private static final String TASK_QUEUE_NAME = "task_queue";

	public static void main(String[] argv)
			throws java.io.IOException, TimeoutException {

		ConnectionFactory factory = new ConnectionFactory();
		factory.setHost("localhost");
		Connection connection = factory.newConnection();
		Channel channel = connection.createChannel();

		channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null);

		String message = "RabbitMQ任务分发机制，发送给一个consumer（consumer-->Worker）";

		/*消息持久化
		1.channel.queueDeclare(TASK_QUEUE_NAME, true, false, false, null);第二个参数为true
		2、MessageProperties.PERSISTENT_TEXT_PLAIN属性
		channel.basicPublish( "", TASK_QUEUE_NAME,MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes());*/
		channel.basicPublish( "", TASK_QUEUE_NAME,
				MessageProperties.PERSISTENT_TEXT_PLAIN,
				message.getBytes());
		System.out.println(" [x] Sent '" + message + "'");

		channel.close();
		connection.close();
	}
	//...
}
